<template>
    <div>
        <Modal
                v-model="addModal"
                title="添加课程"
                :closable="false"
                :mask-closable="false">
            <Form
                    ref="formValidate"
                    :model="formItem"
                    :rules="ruleValidate"
                    :label-width="80">
                <FormItem label="学科" prop="course">
                    <Input v-model="formItem.course" placeholder="课程名称"/>
                </FormItem>
                <FormItem label="手机号" prop="phone">
                    <Input v-model="formItem.phone" placeholder="手机号"/>
                </FormItem>
                <FormItem label="上课老师" prop="nickName">
                    <Input v-model="formItem.nickName" placeholder="上课老师"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="cancel">取消</Button>
                <Button type="primary" size="large" @click="submit('formValidate')">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {userSave} from '../../request/api'

    export default {
        name: "Add",
        data() {
            return {
                formItem: {
                    id: '',
                    nickName: '',
                    phone: '',
                    course: '',
                    roleCode: ''
                },
                ruleValidate: {
                    nickName: [
                        {required: true, message: '名字不能为空', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '手机号不能为空', trigger: 'blur'}
                    ],
                    course: [
                        {required: true, message: '老师不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        props: ['addModal', 'courseModify'],
        watch: {
            courseModify: function () {
                this.formItem = this.courseModify;
            }
        },
        methods: {
            cancel() {
                this.$parent.cancelAddModel()
            },
            submit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.formItem.roleCode = 'teacher'
                        userSave(this.formItem).then((res) => {
                            if (this.formItem.id != '') {
                                this.$parent.cancelAddModel();
                            }
                            if (res.code == 200) {
                                this.$Message.success('保存成功');
                                this.$refs[name].resetFields();
                                this.$parent.getCourList();
                            } else
                                this.$Message.error(res.msg);
                        })
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>
